﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
   public class Find
    {
        // 获取系统配置信息
        public static Models.Config GetConfigInfo()
        {
            DataTable dt = DAL.Query.GetConfigInfo();
            Models.Config config = new Models.Config();
            if (dt.Rows.Count > 0)
            {
                config.WebsiteName = dt.Rows[0]["WebsiteName"].ToString();
                config.WebsiteUrl = dt.Rows[0]["WebsiteUrl"].ToString();
                config.Developer = dt.Rows[0]["Developer"].ToString();
                config.VersionInfo = dt.Rows[0]["VersionInfo"].ToString();
                config.HelpInfo = dt.Rows[0]["HelpInfo"].ToString();
            }
            else
            {
                config.WebsiteName = "DYT通用网站后台管理系统";
                config.VersionInfo = "1.0";
                config.HelpInfo = "邓宜桐个人开发出品！联系电话：13512797346（邓先生）";
            }
            return config;
        }
        // 查找指定模型
        public static Models.Model FindModel(string ID)
        {
            #region 输入合法性判断
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 组装模型集合
            DataTable dt = DAL.Query.QueryOne("tb_model", id);
            if (dt.Rows.Count > 0)
            {
                Models.Model model = new Models.Model();

                model.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                model.Name = dt.Rows[0]["Name"].ToString();
                model.AdminDir = dt.Rows[0]["AdminDir"].ToString();
                model.WebDir = dt.Rows[0]["WebDir"].ToString();
                model.TableName = dt.Rows[0]["TableName"].ToString();
                model.Remark = dt.Rows[0]["Remark"].ToString();

                return model;
            }
            #endregion

            return null;
        }
       //查找指定日志
        public static Models.Log FindLog(string ID)
        {
            #region 输入合法性判断
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 组装模型集合
            DataTable dt = DAL.Query.QueryOne("tb_log", id);
            if (dt.Rows.Count > 0)
            {
                Models.Log log = new Models.Log();

                log.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                log.UserName = dt.Rows[0]["UserName"].ToString();
                log.Time = Convert.ToDateTime(dt.Rows[0]["Time"]);
                log.Action = dt.Rows[0]["Action"].ToString();

                return log;
            }
            #endregion

            return null;
        }
        // 查找指定用户
        public static Models.User FindUser(string ID)
        {
            #region 输入合法性判断
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 组装用户集合
            DataTable dt = DAL.Query.QueryOne("tb_user", id);
            if (dt.Rows.Count > 0)
            {
                Models.User user = new Models.User();

                user.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                user.Name = dt.Rows[0]["Name"].ToString();
                user.ParentID = Convert.ToInt32(dt.Rows[0]["ParentID"].ToString());
                user.Password = dt.Rows[0]["Password"].ToString();
                user.RoleID = Convert.ToInt32(dt.Rows[0]["RoleID"].ToString());
                user.UserEnabled = Convert.ToBoolean(dt.Rows[0]["UserEnabled"].ToString());

                return user;
            }
            #endregion

            return null;
        }
        // 查找指定用户名和密码的用户
        public static Models.User FindUser(string Name, string Password)
        {
            #region 输入合法性判断
            if (Name == "" || Password == "") return null;

            #endregion

            #region 组装用户集合
            DataTable dt = DAL.Query.QueryOneUser(Name, Utility.Tool.SHA256Encrypt(Password));
            if (dt.Rows.Count > 0)
            {
                Models.User user = new Models.User();

                user.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                user.Name = dt.Rows[0]["Name"].ToString();
                user.ParentID = Convert.ToInt32(dt.Rows[0]["ParentID"].ToString());
                user.Password = dt.Rows[0]["Password"].ToString();
                user.RoleID = Convert.ToInt32(dt.Rows[0]["RoleID"].ToString());
                user.UserEnabled = Convert.ToBoolean(dt.Rows[0]["UserEnabled"].ToString());

                return user;
            }
            #endregion

            return null;

        }
        // 查找指定角色
        public static Models.Role FindRole(string ID)
        {
            #region 输入合法性判断
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 组装角色集合
            DataTable dt = DAL.Query.QueryOne("tb_role", id);
            if (dt.Rows.Count > 0)
            {
                Models.Role role = new Models.Role();

                role.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                role.Name = dt.Rows[0]["Name"].ToString();
                role.RoleInfoModelID = Convert.ToInt32(dt.Rows[0]["RoleInfoModelID"]);
                role.IsAdmin = Convert.ToBoolean(dt.Rows[0]["IsAdmin"]);
                role.ModelAdmin = Convert.ToBoolean(dt.Rows[0]["ModelAdmin"]);
                role.ColumnAdmin = Convert.ToBoolean(dt.Rows[0]["ColumnAdmin"]);
                role.ContentAdmin = dt.Rows[0]["ContentAdmin"].ToString();
                role.UserAdmin = Convert.ToBoolean(dt.Rows[0]["UserAdmin"]);
                role.RoleAdmin = Convert.ToBoolean(dt.Rows[0]["RoleAdmin"]);
                role.FileAdmin = Convert.ToBoolean(dt.Rows[0]["FileAdmin"]);
                role.LogAdmin = Convert.ToBoolean(dt.Rows[0]["LogAdmin"]);
                role.SystemAdmin = Convert.ToBoolean(dt.Rows[0]["SystemAdmin"]);
                role.RoleEnabled = Convert.ToBoolean(dt.Rows[0]["RoleEnabled"]);
                return role;
            }
            #endregion

            return null;
        }
        // 查找指定栏目id的所有子栏目
        public static List<Models.Column> FindSubColumn(string parentId)
        {
            #region 输入合法性判断
            int pid;
            try
            {
                pid = Convert.ToInt32(parentId);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 组装栏目集合
            DataTable dt = DAL.Query.QueryColumnALL(pid);
            List<Models.Column> list = new List<Models.Column>();
            for (int i = 0; i < dt.Rows.Count; ++i)
            {
                Models.Column e = new Models.Column();
                e.ID = Convert.ToInt32(dt.Rows[i]["ID"]);
                e.Name = dt.Rows[i]["Name"].ToString();
                e.ParentID = Convert.ToInt32(dt.Rows[i]["ParentID"]);
                e.ModelID = Convert.ToInt32(dt.Rows[i]["ModelID"]);
                e.Remark = dt.Rows[i]["Remark"].ToString();
                list.Add(e);
            }
            #endregion

            return list;
        }
        // 查找指定id的栏目
        public static Models.Column FindOneColumn(string ID)
        {
            #region 输入合法性检查
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 组装栏目成类的实例
            DataTable dt = DAL.Query.QueryOne("tb_column", id);
            if (dt.Rows.Count > 0)
            {
                Models.Column column = new Models.Column();
                column.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                column.Name = dt.Rows[0]["Name"].ToString();
                column.ParentID = Convert.ToInt32(dt.Rows[0]["ParentID"]);
                column.ModelID = Convert.ToInt32(dt.Rows[0]["ModelID"]);
                column.Remark = dt.Rows[0]["Remark"].ToString();
                return column;
            }
            #endregion

            return null;
        }
        // 查找指定id的文章模型
        public static Models.ModelArticle FindArticle(string ID)
        {
            #region 输入合法性检查
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 进行文章模型组装
            DataTable dt = DAL.Query.QueryOne("tb_content_article", id);
            if (dt.Rows.Count > 0)
            {
                Models.ModelArticle ma = new Models.ModelArticle();
                ma.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                ma.ColumnID = Convert.ToInt32(dt.Rows[0]["ColumnID"]);
                ma.FirstEditor = dt.Rows[0]["FirstEditor"].ToString();
                ma.LastEditor = dt.Rows[0]["LastEditor"].ToString();
                ma.Title = dt.Rows[0]["Title"].ToString();
                ma.Content = dt.Rows[0]["Content"].ToString();
                ma.UpdateDate = Convert.ToDateTime(dt.Rows[0]["UpdateDate"]);
                ma.Status = Convert.ToBoolean(dt.Rows[0]["Status"]);
                ma.Source = dt.Rows[0]["Source"].ToString();
                ma.Click = Convert.ToInt32(dt.Rows[0]["Click"]);
                return ma;
            }
            #endregion

            return null;
        }
        // 查找指定id的文章的上下篇
        public static Models.ModelArticle FindUpandDown(string ifWord,int Cid,string sort)
        {         
            #region 进行文章模型组装
            DataTable dt = DAL.Query.QueryIfByCid("top 1 *", "tb_content_article", Cid, ifWord, "UpdateDate", sort);
            if (dt.Rows.Count > 0)
            {
                Models.ModelArticle ma = new Models.ModelArticle();
                ma.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                ma.ColumnID = Convert.ToInt32(dt.Rows[0]["ColumnID"]);
                ma.FirstEditor = dt.Rows[0]["FirstEditor"].ToString();
                ma.LastEditor = dt.Rows[0]["LastEditor"].ToString();
                ma.Title = dt.Rows[0]["Title"].ToString();
                ma.Content = dt.Rows[0]["Content"].ToString();
                ma.UpdateDate = Convert.ToDateTime(dt.Rows[0]["UpdateDate"]);
                ma.Status = Convert.ToBoolean(dt.Rows[0]["Status"]);
                ma.Source = dt.Rows[0]["Source"].ToString();
                ma.Click = Convert.ToInt32(dt.Rows[0]["Click"]);
                return ma;
            }
            #endregion

            return null;
        }

        // 查找指定id的单页模型
        public static Models.ModelContent FindContent(string ID)
        {
            #region 输入合法性检查
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 进行单页模型组装
            DataTable dt = DAL.Query.QuerySomeByCid("tb_content", id,"ID","asc");
            if (dt.Rows.Count > 0)
            {
                Models.ModelContent mc = new Models.ModelContent();
                mc.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                mc.FirstEditor = dt.Rows[0]["FirstEditor"].ToString();
                mc.LastEditor = dt.Rows[0]["LastEditor"].ToString();
                mc.ColumnID = Convert.ToInt32(dt.Rows[0]["ColumnID"]);
                mc.Title = dt.Rows[0]["Title"].ToString();
                mc.Content = dt.Rows[0]["Content"].ToString();
                mc.UpdateDate = Convert.ToDateTime(dt.Rows[0]["UpdateDate"]);
                return mc;
            }
            #endregion

            return null;
        }

        // 查找指定id的外链模型
        public static Models.ModelLink FindLink(string ID)
        {
            #region 输入合法性检查
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 进行滑动图片模型组装
            DataTable dt = DAL.Query.QueryOne("tb_content_link", id);
            if (dt.Rows.Count > 0)
            {
                Models.ModelLink ms = new Models.ModelLink();
                ms.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                ms.ColumnID = Convert.ToInt32(dt.Rows[0]["ColumnID"]);
                ms.FirstEditor = dt.Rows[0]["FirstEditor"].ToString();
                ms.LastEditor = dt.Rows[0]["LastEditor"].ToString();
                ms.WebUrl = dt.Rows[0]["WebUrl"].ToString();
                ms.UpdateDate = Convert.ToDateTime(dt.Rows[0]["UpdateDate"]);
                ms.WebName = dt.Rows[0]["WebName"].ToString();
                ms.WebLogo = dt.Rows[0]["WebLogo"].ToString();       
                return ms;
            }
            #endregion

            return null;
        }

        // 查找指定id的滑动图片模型
        public static Models.ModelSlider FindSlider(string ID)
        {
            #region 输入合法性检查
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 进行外链模型组装
            DataTable dt = DAL.Query.QueryOne("tb_content_slider", id);
            if (dt.Rows.Count > 0)
            {
                Models.ModelSlider ms = new Models.ModelSlider();
                ms.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                ms.ColumnID = Convert.ToInt32(dt.Rows[0]["ColumnID"]);
                ms.FirstEditor = dt.Rows[0]["FirstEditor"].ToString();
                ms.LastEditor = dt.Rows[0]["LastEditor"].ToString();
                ms.Title = dt.Rows[0]["Title"].ToString();
                ms.UpdateDate = Convert.ToDateTime(dt.Rows[0]["UpdateDate"]);
                ms.PictureAddress = dt.Rows[0]["PictureAddress"].ToString();
                ms.ArticleAddress = dt.Rows[0]["ArticleAddress"].ToString();
                ms.Remark = dt.Rows[0]["Remark"].ToString();
                return ms;
            }
            #endregion

            return null;
        }
        // 查找指定id的办公文件模型
        public static Models.ModelOfficeFile FindOfficeFile(string ID)
        {
            #region 输入合法性检查
            int id;
            try
            {
                id = Convert.ToInt32(ID);
            }
            catch
            {
                return null;
            }
            #endregion

            #region 进行办公文件模型组装
            DataTable dt = DAL.Query.QueryOne("tb_content_officefile", id);
            if (dt.Rows.Count > 0)
            {
                Models.ModelOfficeFile ms = new Models.ModelOfficeFile();
                ms.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                ms.ColumnID = Convert.ToInt32(dt.Rows[0]["ColumnID"]);
                ms.FileName = dt.Rows[0]["FileName"].ToString();
                ms.FileEditor = dt.Rows[0]["FileEditor"].ToString();
                ms.FileType = dt.Rows[0]["FileType"].ToString();
                ms.FileSize = dt.Rows[0]["FileSize"].ToString();            
                ms.FileUrl = dt.Rows[0]["FileUrl"].ToString();
                ms.UploadTime = Convert.ToDateTime(dt.Rows[0]["UploadTime"]);
                ms.UploadSource = dt.Rows[0]["UploadSource"].ToString();
                return ms;
            }
            #endregion

            return null;
        }
    }
}
